<?php
class PNUserPostsArray extends PNObjectExArray {
  function PNUserPostsArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'innoforum_posts';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->addCountView();

    $this->_init($init, $where);
  }
  function addCountView(){
    $id = FormUtil::getPassedValue ('id', false);
    if ($id){
      $max = DBUtil::selectFieldMax('innoforum_topics','count_view' ,'MAX', "WHERE tpc_id = '$id'");
      $object = array('id'=>$id , 'count_view'=>intval($max) + 1);
      DBUtil::updateObject($object,'innoforum_topics');
    }
  }
  function insertPostProcess(){
    $form = FormUtil::getPassedValue ('form', false);
    $is_sendmail = $form['is_sendmail'];
    if ($is_sendmail ){
      //TODO : send mail when new post
    }
  }

  function selectExtendResult(){
    $id = FormUtil::getPassedValue ('id', false);
    $result = array();
    if ($id){
      $result['topics'] = DBUtil::selectObject('innoforum_topics', "WHERE tpc_id = '$id'");
      $result['type'] = DBUtil::selectObject('innoforum_type', "WHERE tpy_id = '{$result['topics']['type_id']}'");
      $result['count_post'] = DBUtil::selectObjectCount('innoforum_posts', "WHERE pst_topics_id = '$id'") + 1;

      $where = "WHERE rsi_path = 'topics' AND rsi_referer_id = '$id' ";
      $result['images'] = DBUtil::selectObjectArray('innoforum_resource_image' , "WHERE rsi_referer_id = '$id' AND rsi_path = 'topics'" , '', -1, -1,
                                '', null, null, array('id','filename','thumbname'));

      $result['firstimages'] = DBUtil::selectObject('innoforum_topics' , "WHERE tpc_id = '$id'" ,array('id','firstimage','firsthumbimage'));

      //set <!--[keyword]-->
      if(!empty($result['topics']['title'])){
        PageUtil::setVar('title', $result['topics']['title'].", รหัส ".$result['topics']['ref_id']);
      }
			
      $result['finance'] = DBUtil::selectObjectArray('innoforum_finance' , "WHERE fin_topic_id = '$id'" , '', -1, -1, '', null, null, array(
                                                                                                                                      'id', 
                                                                                                                                      'topic_id', 
                                                                                                                                      'uid', 
                                                                                                                                      'finance_telephone', 
                                                                                                                                      'finance_email', 
                                                                                                                                      'company_name', 
                                                                                                                                      'balance', 
                                                                                                                                      'interest', 
                                                                                                                                      'min_installment',
                                                                                                                                      'max_installment',
                                                                                                                                      'vat'
                                                                                                                                      )
                                                    );
    //var_dump ($result['finance']);
    //exit;
    }
    return $result;
  }

  function genFilter(){
    $id = FormUtil::getPassedValue ('id', false);
    $where = '';
    if ($id){
      $where = "WHERE pst_topics_id = '$id'";
    }
    return $where;
  }
  function genSort(){
    return "ORDER BY pst_id DESC";
  }
  
  function genLimit(){
  return 10;
  }
}